package com.example.assetmanagement.controller;

import com.example.assetmanagement.common.ApiResponse;
import com.example.assetmanagement.common.PageRequest;
import com.example.assetmanagement.common.PageResponse;
import com.example.assetmanagement.domain.OperationLog;
import com.example.assetmanagement.service.OperationLogService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/operation-log")
@RequiredArgsConstructor
public class OperationLogController {

    private final OperationLogService logService;

    @GetMapping("/list")
    public ApiResponse<PageResponse<OperationLog>> list(@Valid PageRequest pageRequest,
                                                        @RequestParam(required = false) String module,
                                                        @RequestParam(required = false) String operationType,
                                                        @RequestParam(required = false) Long userId) {
        return ApiResponse.success(logService.list(pageRequest, module, operationType, userId));
    }

    @GetMapping("/{id}")
    public ApiResponse<OperationLog> detail(@PathVariable Long id) {
        return ApiResponse.success(logService.getById(id));
    }

    @DeleteMapping("/{id}")
    public ApiResponse<?> delete(@PathVariable Long id) {
        logService.delete(id);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/batch")
    public ApiResponse<?> batchDelete(@RequestBody List<Long> ids) {
        logService.batchDelete(ids);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/clear")
    public ApiResponse<?> clear() {
        logService.clear();
        return ApiResponse.success(null);
    }

    @GetMapping("/stats")
    public ApiResponse<Map<String, Object>> stats() {
        return ApiResponse.success(logService.stats());
    }

    @GetMapping("/operation-type-stats")
    public ApiResponse<List<Map<String, Object>>> opTypeStats() {
        return ApiResponse.success(logService.operationTypeStats());
    }

    @GetMapping("/module-stats")
    public ApiResponse<List<Map<String, Object>>> moduleStats() {
        return ApiResponse.success(logService.moduleStats());
    }
} 